package cn.bus.controller;

import cn.bus.service.CustomerService;
import cn.bus.vo.CustomerVo;
import cn.sys.utils.DataGridView;
import cn.sys.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Mxb
 * @version 1.0
 * @date 2019/11/2 21:18
 * 控制器
 */
@RestController
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo) {
        return this.customerService.queryAllCustomer(customerVo);
    }

    //添加客户
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo customerVo) {
        try {
            //自动创建时间
            customerVo.setCreatetime(new Date());
            this.customerService.addCustomer(customerVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception d) {
            d.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    //修改客户
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(CustomerVo customerVo) {
        try {
            this.customerService.updateCustomer(customerVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception d) {
            d.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    //根据id删除客户
    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(CustomerVo customerVo) {
        try {
            this.customerService.deleteCustomerById(customerVo.getIdentity());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception d) {
            d.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除客户
    @RequestMapping("deleteBatchCustomer")
    public ResultObj deleteBatchCustomer(CustomerVo customerVo) {
        try {
            this.customerService.deleteBatchCustomer(customerVo.getIds());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception d) {
            d.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


}
